package com.work.straighttalk.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMImageMessageBody;
import com.hyphenate.chat.EMMessage;
import com.work.straighttalk.R;
import com.work.straighttalk.app.StraightTalk;
import com.work.straighttalk.bean.StraightTalkMessage;
import com.work.straighttalk.model.PicUriTransformer;

import butterknife.Bind;

/**
 * 用于加载图片的holder类
 * Created by 朱相卿 on 16/3/19.
 */
public class ChatImageHolder extends BaseViewHolder<StraightTalkMessage> {
    @Bind(R.id.sd_chat_pic)
    SimpleDraweeView sdChatPic;
    @Bind(R.id.sd_chat_pic_progress)
    ProgressBar sdChatPicProgress;

    public ChatImageHolder(Context context, ViewGroup root) {
        super(context, root, R.layout.item_chat_picture_send);
    }


    @Override
    public void bindData(StraightTalkMessage myMessage) {
        EMMessage emMessage = myMessage.getEmMessage();
        emMessage.setMessageStatusCallback(new EMCallBack() {
            @Override
            public void onSuccess() {
                ((Activity) getContext()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        sdChatPicProgress.setVisibility(View.INVISIBLE);
                    }
                });
            }

            @Override
            public void onError(int i, String s) {
                ((Activity) getContext()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        sdChatPicProgress.setVisibility(View.GONE);
                    }
                });
            }

            @Override
            public void onProgress(int i, String s) {
                ((Activity) getContext()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        sdChatPicProgress.setVisibility(View.VISIBLE);
                    }
                });
            }
        });
        EMImageMessageBody body = (EMImageMessageBody) emMessage.getBody();
        PicUriTransformer transformer = new PicUriTransformer(StraightTalk.applicationContext);
        Uri uri = transformer.FilePathToUri(body.getLocalUrl());
        sdChatPic.setImageURI(uri);
    }
}
